package com.hyw.cm.protocal.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

/**
 * 一个简易日志打印工具
 * 
 * @author Hongyu
 */
public class LoggingDuplexHandler extends ChannelDuplexHandler {

  private static final Logger log = LoggerFactory.getLogger(LoggingDuplexHandler.class);

  public LoggingDuplexHandler() {
    super();
  }

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    String dump = ByteBufUtil.hexDump((ByteBuf) msg).toUpperCase();
    log.warn("Recv:{}", dump);
    super.channelRead(ctx, msg);
  }

  @Override
  public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    String dump = ByteBufUtil.hexDump((ByteBuf) msg).toUpperCase();
    log.warn("Sent:{}", dump);
    super.write(ctx, msg, promise);
  }

}
